package my.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import my.jdbc.DbConUtil;
import my.jdbc.JdbcTemplate;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import bean.KeyValue;

@Controller
@RequestMapping("oils")
public class OilsUpdateToolsAction {

	@RequestMapping("index")
	@ResponseBody
	public Object indexAction(HttpSession session, String ckdNo, String ipAddress) {
		List<Map<String, Object>> youpinInfoList = new ArrayList<Map<String, Object>>();

		if (ckdNo == null || ipAddress == null) {
			return youpinInfoList;
		}

		// 在站里面已经开始进货验收的不能修改
		String zhanJhysSql = " SELECT * FROM oss_acceptance_odRegister WHERE DeliveryNo = '" + ckdNo + "' ";

		Object[] appJhysInfo = JdbcTemplate.queryFieldInfoByFields(DbConUtil.getGsmappCon(ipAddress), zhanJhysSql, null, ipAddress);

		// 查询平台对应的油品编码
		String pingtaiSql = " SELECT ckd.oils_id,oils.cn_name FROM SH_CKD ckd,SH_OILS oils WHERE ckd.oils_id = oils.oils_id and ckd.ck_id = '" + ckdNo + "' ";
		Object[] pingtaiCkdInfo = JdbcTemplate.queryFieldInfoByFields(DbConUtil.get162ptCon(), pingtaiSql, null, DbConUtil.TYPE_NAME_162PT);

		// 查询北分中心的油品编码
		String bfCenterSql = " SELECT * FROM oss_acceptance_deliveryBill WHERE DeliveryNo = '" + ckdNo + "' ";
		Object[] bfCenterInfo = JdbcTemplate.queryFieldInfoByFields(DbConUtil.getGsmcenterCon(), bfCenterSql, null, DbConUtil.TYPE_NAME_GSMCENTER);

		// 查询站里面的油品编码
		String zhanYoupinSql = " SELECT ckd.OilNo,oils.OilName FROM oss_acceptance_deliveryBill ckd,oss_sysmanage_oilType oils WHERE ckd.OilNo = oils.OilNo and ckd.DeliveryNo = '" + ckdNo + "' ";
		Object[] zhanInfo = JdbcTemplate.queryFieldInfoByFields(DbConUtil.getGsmappCon(ipAddress), zhanYoupinSql, null, ipAddress);

		Map<String, Object> youpinInfoMap = new HashMap<String, Object>();
		youpinInfoMap.put("ckdNo", ckdNo);

		if (pingtaiCkdInfo != null) {
			youpinInfoMap.put("pingtaiOilsNo", pingtaiCkdInfo[0]);
			youpinInfoMap.put("pingtaiOilsName", pingtaiCkdInfo[1]);
		}
		if (bfCenterInfo != null) {
			youpinInfoMap.put("bfCenterOilsNo", bfCenterInfo[5]);
		}
		if (zhanInfo != null) {
			youpinInfoMap.put("appOilsNo", zhanInfo[0]);
			youpinInfoMap.put("appOilsName", zhanInfo[1]);
		}

		youpinInfoMap.put("yanshouState", appJhysInfo != null ? "已开始" : "未开始");

		youpinInfoList.add(youpinInfoMap);

		return youpinInfoList;
	}

	@RequestMapping("update")
	@ResponseBody
	public Object updateAction(String ckdNo, String ipAddress, String oilNo) {
		// 修改北分中心
		String updateBfCenterSql = "UPDATE oss_acceptance_deliveryBill SET OilNo = '" + oilNo + "' WHERE DeliveryNo = '" + ckdNo + "' ";
		JdbcTemplate.updateByFields(DbConUtil.getGsmcenterCon(), updateBfCenterSql, null, DbConUtil.TYPE_NAME_GSMCENTER);

		// 修改站级
		String appSql = "UPDATE oss_acceptance_deliveryBill SET OilNo = '" + oilNo + "' WHERE DeliveryNo = '" + ckdNo + "' ";
		JdbcTemplate.updateByFields(DbConUtil.getGsmappCon(ipAddress), appSql, null, ipAddress);

		return true;

	}

	@RequestMapping("oilsRef")
	@ResponseBody
	public Object selectOilsRefList() {
		String oilsRefSql = "SELECT * FROM BF_OILS_REF ";
		List<Object[]> oilsRefList = JdbcTemplate.queryListFieldInfoByFields(DbConUtil.get162ptCon(), oilsRefSql, null, DbConUtil.TYPE_NAME_162PT);
		Map<String, List<KeyValue>> oilsRefMap = new HashMap<String, List<KeyValue>>();
		for (Object[] objects : oilsRefList) {
			KeyValue kv = new KeyValue();
			kv.setId(objects[1] + "");
			kv.setText(objects[2] + "");
			if (oilsRefMap.containsKey(objects[0] + "")) {
				oilsRefMap.get(objects[0] + "").add(kv);
			} else {
				List<KeyValue> kvList = new ArrayList<KeyValue>();
				kvList.add(kv);
				oilsRefMap.put(objects[0] + "", kvList);
			}
		}
		return oilsRefMap;
	}

}
